package Demo6;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

public class select {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
//		System.out.println("请输入查询列名字");
//		String str1 = scanner.next();
		System.out.println("请输入查询条件");
		String str2 = scanner.next();
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			Connection connection = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=glxt", "sa", "sa123456");
			String sql = "select * from yh where yhmz = ?";

			// 指令预处理
			PreparedStatement statement = connection.prepareStatement(sql);
			// ? 占位符
			// 设置占位符           占位符的序号 从1开始
//			statement.setString(1,str1);
			statement.setString(1,str2);

			// 获取结果集
			ResultSet resultSet = statement.executeQuery();

			while(resultSet.next()) {
				String name = resultSet.getString(1);
				String mm = resultSet.getString(2);
				int jb = resultSet.getInt(3);
				System.out.println("账号： " + name +" 密码： " + mm + " 级别： " + jb);
			}
			//7.关闭
			resultSet.close();
			statement.close();
			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}